package com.gs.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

//import org.apache.log4j.Logger;

public class PropertyUtil {
	//static Logger log = Logger.getLogger(PropertyUtil.class);
	static Properties prop;
	static{	
		prop = new Properties();     
	}
	public static Map<String, String> redProFile(String path){	
		InputStream in=null;
		try {
			//读取属性文件a.properties
			 in = new BufferedInputStream (
					new FileInputStream(path));
			prop.load(in);     ///加载属性列表
			Map<String, String> map=new HashMap<String, String>();
			Iterator<String> it=prop.stringPropertyNames().iterator();
			while(it.hasNext()){
				String key=it.next();
				map.put(key, prop.getProperty(key));
			//	System.out.println(key+":"+prop.getProperty(key));
			}
			return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//log.error(e);
		/*	e.printStackTrace();*/
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//log.error(e);
		//	e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//log.error(e);
			}
		}
		return null;
	}

	public static boolean writerProFile(String path,boolean append,Map<String, String> map){
		System.out.println(path);
		InputStream in=null;
		FileOutputStream oFile=null;
		try {
			//读取属性文件a.properties
			 in = new BufferedInputStream (
					new FileInputStream(path));
			prop.load(in);     ///加载属性列表
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//log.error(e);
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//log.error(e);
			return false;
		}finally{
			try {
				if(in!=null)
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//log.error(e);
			}
		}
		try {
			///保存属性到properties文件
			
			if(map!=null){
				Iterator entries = map.entrySet().iterator();  		  
				while (entries.hasNext()) {  	  
				    Map.Entry entry = (Map.Entry) entries.next();  		  
				    String key = (String)entry.getKey();  			  
				    String value = (String)entry.getValue();  
				    System.out.println(key+"----->"+value);
				    prop.setProperty(key,value);
				}  
				
			}	
			oFile = new FileOutputStream(path, append);//true表示追加打开
			prop.store(oFile, "The New properties file");
			//oFile.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//log.error(e);
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//log.error(e);
			return false;
		}finally{
			try {
				if(oFile!=null)
					oFile.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//log.error(e);
			}
			
		}
		return true;
	}

}
